package com.sfc.spiderweb.util;

import org.jsoup.Jsoup;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {
    private static final String timeUrl = "http://47.112.12.109:8443/user-service/custom/time";
    private static String netPattern = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前的年月日,英文的
     *
     * @return
     */
    public static String[] currencyDate() {
        String[] dateStr = new String[3];
        Date date = new Date();
        String dateString = date.toString();
        // xx:xx:xx
        int length = dateString.length();
        dateStr[0] = dateString.substring(length - 4, length);
        dateStr[1] = dateString.substring(4, 7);
        dateStr[2] = dateString.substring(8, 10);
        return dateStr;
    }

    /**
     * 获取某月的月份英文
     *
     * @return
     */
    public static String englishMonthName(Date nowDate) {
        SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
        // 获取当前日期
        Calendar cal_1 = Calendar.getInstance();
        cal_1.setTime(nowDate);
        // 设置为1号,当前日期既为本月第一天
        cal_1.set(Calendar.DAY_OF_MONTH, 1);
        String firstDay = format.format(cal_1.getTime());
        return firstDay.substring(8, 11);
    }

    /**
     * 获取上个月的月份英文
     *
     * @return
     */
    public static String lastMonth(Date nowDate) {
        SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
        format.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));
        // 获取当前日期
        Calendar cal_1 = Calendar.getInstance();
        cal_1.setTime(nowDate);
        cal_1.add(Calendar.MONTH, -1);
        // 设置为1号,当前日期既为本月第一天
        cal_1.set(Calendar.DAY_OF_MONTH, 1);
        String firstDay = format.format(cal_1.getTime());
        return firstDay.substring(8, 11);
    }

    /**
     * 获取上个月的中文月份
     *
     * @return
     */
    public static String getlastMonthChineses(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("MMM", Locale.CHINESE);
        format.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));
        // 获取前月的第一天
        Calendar cal_1 = Calendar.getInstance();// 获取当前日期
        cal_1.setTime(date);
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        String firstDay = format.format(cal_1.getTime());
        return firstDay;
    }


    /**
     * 获取年份
     *
     * @param date
     * @return
     */
    public static String getYear(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        return format.format(date);
    }

    /**
     * 获取上个月的月份，数字的
     *
     * @return
     */
    public static String lastMonthNum() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar cl = Calendar.getInstance();
        // cl.setTime(dateNow);
        // cl.add(Calendar.DAY_OF_YEAR, -1);
        // 一天
        // cl.add(Calendar.WEEK_OF_YEAR, -1);
        // 一周
        cl.add(Calendar.MONTH, -1);
        // 从现在算，之前一个月,如果是2个月，那么-1-----》改为-2
        Date dateFrom = cl.getTime();
        return sdf.format(dateFrom).substring(4, 6);
    }

    /**
     * 获取某月的
     *
     * @param date
     * @return
     */
    public static String getLastYearAndMonth(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1);
        return format.format(cal.getTime());
    }

    /**
     * 获取年月日
     *
     * @return
     */
    public static String getDate(String dataFormat, Date date) {
        SimpleDateFormat df = new SimpleDateFormat(dataFormat);// 设置日期格式
        String format = df.format(date);
        return format;
    }

    /**
     * 格式化某时间
     *
     * @param format
     * @param date
     * @return
     */
    public static String formatDate(String format, Date date) {
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }

    /**
     * 获取上个月的第一天和最后一天
     *
     * @return
     */
    public static String[] getLastMonthFirstEnd(String dataFormat, Date date) {
        String[] lastend = new String[2];
        // 设置日期格式
        SimpleDateFormat sdf = new SimpleDateFormat(dataFormat);
        // 获取前一个月第一天
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date);
        calendar1.add(Calendar.MONTH, -1);
        calendar1.set(Calendar.DAY_OF_MONTH, 1);
        String firstDay = sdf.format(calendar1.getTime());
        lastend[0] = firstDay;
        // 获取前一个月最后一天
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date);
        calendar2.set(Calendar.DAY_OF_MONTH, 0);
        String lastDay = sdf.format(calendar2.getTime());
        lastend[1] = lastDay;
        return lastend;
    }

    /**
     * 获取当月的第一天
     *
     * @return
     */
    public static String getCurrentMonthFirstDay(String dataFormat, Date date) {
        // 获取当前月第一天：
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        String first = new SimpleDateFormat(dataFormat).format(c.getTime());
        return first;
    }


    /**
     * 获取上个月的最后一天的日期
     *
     * @return
     */
    public static String getgetLastMonthEnd(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd");// 设置日期格式
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date);
        calendar2.set(Calendar.DAY_OF_MONTH, 0);
        String lastDay = sdf.format(calendar2.getTime());
        return lastDay;
    }

    /**
     * 获取昨天的日期
     *
     * @return
     */
    public static String getYesterday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, -1);
        String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return yesterday;
    }

    /**
     * 获取一个星期之前的日期
     *
     * @return
     */
    public static String getWeekday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, -7);
        String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return yesterday;
    }

    public static String getThisWeek(Date date){
        SimpleDateFormat dateFm = new SimpleDateFormat("EEEE",Locale.CHINESE);
        String currSun = dateFm.format(date);
        return currSun;
    }

    /**
     * 获取每个星期四的日期
     *
     * @param date
     * @return
     */
    public static String getThisWeekTHURSDAY(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.THURSDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
        String format = df.format(cal.getTime());
        return format;
    }

    public static Date parse(String date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        // 从现在算，之前一个月,如果是2个月，那么-1-----》改为-2
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String format(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        // 从现在算，之前一个月,如果是2个月，那么-1-----》改为-2
        Date dateFrom = cl.getTime();
        return sdf.format(dateFrom);
    }

    public static String getNextMonth(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(Calendar.MONTH, 1);
        // 从现在算，之前一个月,如果是2个月，那么-1-----》改为-2
        Date dateFrom = cl.getTime();
        return sdf.format(dateFrom);
    }


    public static String getLastMonth(String dateStr, String pattern) {
        return getLastMonth(parse(dateStr, netPattern), pattern);
    }

    public static String getLastMonth(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(Calendar.MONTH, -1);
        // 从现在算，之前一个月,如果是2个月，那么-1-----》改为-2
        Date dateFrom = cl.getTime();
        return sdf.format(dateFrom);
    }

    public static String getLastDay(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(Calendar.DAY_OF_MONTH, -1);
        return sdf.format(cl.getTime());
    }

    public static String getNetTime() {
        String result = "";
        try {
            result = Jsoup.connect(timeUrl).execute().body();
        } catch (IOException e) {
            e.printStackTrace();
            result = format(new Date(), netPattern);
        }
        return result;
    }

}
